package com.wan37.logic.listener.handler;

import com.wan37.logic.shop.domain.vo.CommodityConfigVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.listener.annotation.EventHandler;
import com.wan37.logic.listener.annotation.Listener;
import com.wan37.logic.listener.event.EventType;
import com.wan37.logic.listener.event.user.UserLoginEvent;
import com.wan37.logic.shop.cache.CommodityServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.CommonShop;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-06 16:04
 **/
@Listener
public class CommodityListenerHandler {
    private RouteUtils routeUtils;
    private CommodityServiceCache commodityServiceCache;

    public CommodityListenerHandler() {
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.commodityServiceCache = SpringContextHolder.getBean("commodityServiceCache");
    }

    @EventHandler(value = EventType.LOGIN)
    public void login(UserLoginEvent userLoginEvent) {
        List<CommodityConfigVO> commodityConfigVOList = new ArrayList<>();
        commodityServiceCache.getCommodityInMap().values()
                .forEach(commodityIn -> commodityConfigVOList.add(commodityIn.getCommodityConfigVO()));
        routeUtils.sendShopResponse(CommonShop.GAME_GET_ALL_COMMODITY,
                "商城信息初始化成功！", commodityConfigVOList, userLoginEvent.getUserId());
    }

}
